using System;
using System.Collections.Generic;
using System.Linq;
using MesDashboardApi.Api.Models;

namespace MesDashboardApi.Api.Services
{
    public class DashboardDataService
    {
        private DashboardData _dashboardData;

        public DashboardDataService()
        {
            InitializeData();
        }

        private void InitializeData()
        {
            // 初始化仪表盘数据
            _dashboardData = new DashboardData
            {
                // 生产计划数据
                PlanConfig = new PlanConfig
                {
                    Data = new List<PlanData>
                    {
                        new PlanData { Name = "计划A", Value = 100 },
                        new PlanData { Name = "计划A", Value = 100 },
                        new PlanData { Name = "计划A", Value = 100 },
                        new PlanData { Name = "计划A", Value = 100 }
                    },
                    Colors = new List<string> { "#00baff", "#3de7c9", "#ffc530", "#469f4b" },
                    Unit = "%"
                },

                // 设备运行状态数据
                DeviceConfig = new DeviceConfig
                {
                    Data = new List<DeviceData>
                    {
                        new DeviceData { Name = "正常运行", Value = 55 },
                        new DeviceData { Name = "待机中", Value = 25 },
                        new DeviceData { Name = "故障停机", Value = 15 },
                        new DeviceData { Name = "计划检修", Value = 5 }
                    },
                    Color = new List<string> { "#00baff", "#3de7c9", "#ff5722", "#ffc530" }
                },

                // 生产概况数据
                ProductionConfig = new ProductionConfig
                {
                    Count = new NumberConfig
                    {
                        Number = new List<double> { 1234 },
                        Content = "{nt}",
                        Style = new NumberStyle
                        {
                            FontSize = 32,
                            Fill = "#3de7c9"
                        }
                    },
                    Target = new NumberConfig
                    {
                        Number = new List<double> { 2000 },
                        Content = "{nt}",
                        Style = new NumberStyle
                        {
                            FontSize = 32,
                            Fill = "#00baff"
                        }
                    },
                    Rate = new NumberConfig
                    {
                        Number = new List<double> { 99.8 },
                        Content = "{nt}%",
                        Style = new NumberStyle
                        {
                            FontSize = 32,
                            Fill = "#ffc530"
                        }
                    }
                },

                // 生产线站点数据
                ProductionStations = new List<ProductionStation>
                {
                    new ProductionStation { Name = "上料", Active = true, Warning = false },
                    new ProductionStation { Name = "组装", Active = true, Warning = false },
                    new ProductionStation { Name = "测试", Active = true, Warning = true },
                    new ProductionStation { Name = "包装", Active = true, Warning = false },
                    new ProductionStation { Name = "入库", Active = false, Warning = false }
                },

                // 品质监控数据
                QualityConfig = new QualityConfig
                {
                    Value = 98.6,
                    Colors = new List<string> { "#00baff", "rgba(0, 186, 255, 0.2)" },
                    BorderWidth = 3,
                    BorderGap = 5,
                    LineDash = new List<int> { 10, 2 },
                    TextColor = "#fff"
                },

                // 告警信息数据
                AlarmList = new List<AlarmInfo>
                {
                    new AlarmInfo { Time = "10:22:15", Device = "组装线1号", Content = "温度过高", Level = "严重" },
                    new AlarmInfo { Time = "09:45:23", Device = "测试设备2号", Content = "电压不稳定", Level = "中度" },
                    new AlarmInfo { Time = "08:37:41", Device = "包装机3号", Content = "缺料预警", Level = "轻微" },
                    new AlarmInfo { Time = "07:15:58", Device = "上料机4号", Content = "电机异常", Level = "严重" },
                    new AlarmInfo { Time = "06:59:33", Device = "运输带2号", Content = "速度异常", Level = "中度" }
                },

                // 生产趋势数据
                TrendData = new TrendData
                {
                    Months = new List<string> { "1月", "2月", "3月", "4月", "5月", "6月", "7月" },
                    Production = new List<int> { 3200, 3600, 3900, 3600, 4000, 4200, 4500 },
                    Defects = new List<int> { 120, 132, 101, 134, 90, 180, 210 }
                },

                // 当前时间
                CurrentTime = FormatDateTime(DateTime.Now)
            };
        }

        // 获取仪表盘所有数据
        public DashboardData GetDashboardData()
        {
            _dashboardData.CurrentTime = FormatDateTime(DateTime.Now);
            return _dashboardData;
        }

        // 更新生产计划数据 - 返回固定值
        public PlanConfig UpdatePlanData()
        {
            // 直接返回固定数据，不做随机修改
            return _dashboardData.PlanConfig;
        }

        // 更新设备运行状态数据 - 返回固定值
        public DeviceConfig UpdateDeviceData()
        {
            // 直接返回固定数据，不做随机修改
            return _dashboardData.DeviceConfig;
        }

        // 更新生产概况数据 - 返回固定值
        public ProductionConfig UpdateProductionConfig()
        {
            // 直接返回固定数据，不做随机修改
            return _dashboardData.ProductionConfig;
        }

        // 更新生产线站点状态 - 返回固定值
        public List<ProductionStation> UpdateProductionStations()
        {
            // 直接返回固定数据，不做随机修改
            return _dashboardData.ProductionStations;
        }

        // 更新品质监控数据 - 返回固定值
        public QualityConfig UpdateQualityData()
        {
            // 直接返回固定数据，不做随机修改
            return _dashboardData.QualityConfig;
        }

        // 更新告警信息 - 返回固定值
        public List<AlarmInfo> UpdateAlarmList()
        {
            // 添加一条固定的新告警，带当前时间
            string time = DateTime.Now.ToString("HH:mm:ss");
            
            // 每次都替换第一条数据的时间为当前时间，其他保持不变
            if (_dashboardData.AlarmList.Count > 0)
            {
                _dashboardData.AlarmList[0].Time = time;
            }
            
            return _dashboardData.AlarmList;
        }

        // 获取当前时间
        public string GetCurrentTime()
        {
            return FormatDateTime(DateTime.Now);
        }

        // 设置生产计划数据
        public PlanConfig SetPlanData(List<PlanData> newPlanData)
        {
            if (newPlanData != null && newPlanData.Count > 0)
            {
                // 清除现有数据
                _dashboardData.PlanConfig.Data.Clear();
                
                // 添加新数据
                foreach (var item in newPlanData)
                {
                    // 确保值在有效范围内
                    int value = Math.Min(100, Math.Max(0, item.Value));
                    _dashboardData.PlanConfig.Data.Add(new PlanData
                    {
                        Name = item.Name,
                        Value = value
                    });
                }
            }
            
            return _dashboardData.PlanConfig;
        }

        // 格式化日期时间
        private static string FormatDateTime(DateTime date)
        {
            return date.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }
} 